from utils.log import Log


class MapSet(Log):
    # is_map: True map
    # is_map: False set
    def __init__(self, data={}, is_map=True):
        super(MapSet, self).__init__()
        self.data = data
        self.is_map = is_map

    def put(self, key, value=None):
        if self.is_map:
            self.data[key] = value
        else:
            if key in self.data:
                self.data[key] += 1
            else:
                self.data[key] = 1

    def pop(self, key):
        if self.is_map:
            if key in self.data:
                return self.clear(key)
        else:
            if key in self.data:
                self.data[key] -= 1
                if self.data[key] == 0:
                    self.clear(key)

    def clear(self, key):
        if key in self.data:
            return self.data.pop(key)

    def get(self, key):
        if key in self.data:
            return self.data[key]
        else:
            return None


if __name__ == '__main__':
    ms = MapSet()
    print(ms.get("nokey"))
    ms.put("key", "value1")
    print(ms.get("key"))
    ms.put("key", "value2")
    print(ms.get("key"))
    ms.pop("key")
    print(ms.get("key"))

    ms1 = MapSet(is_map=False)
    ms1.put("key")
    ms1.put("key")
    print(ms1.get("key"))
    ms1.pop("key")
    print(ms1.get("key"))
    ms1.pop("key")
    print(ms1.get("key"))
